////////////////////////////////////////////////////////////////////////////////
//////// LEADER TERM LIMITS
////////////////////////////////////////////////////////////////////////////////

/*
This do-file determines if candidates in presidential elections will be confronted with a term limit if they win the election. It draws data from the Comparative Constitutions Project, presidential elections results, and the links between the presidential candidates and Wikipedia.
We only do this work for the two first candidates, and for the representative of the incumbency.
*/

////////////////////////////////////////////////////////////////////////////////
//////// a. Determining the number of previous wins of each candidate
////////////////////////////////////////////////////////////////////////////////

/*
We start by determining for each presidential candidate the number of times they have already been president, and how many times they have been president immediatly before the election, without interruption. We identify presidential candidates with their Wikidata ID.
*/

// Importing election results
use "$project_path/data/3_cleaned/presidential_elections.dta", clear
keep Country Year Month Type_Election Candidate_* flag_not_last flag_inconsequential
reshape long Candidate_, i(Country Year Month flag_not_last flag_inconsequential) j(rank)
rename Candidate_ Candidate
drop if Candidate==""
drop if Candidate=="Party without candidates"
drop if Candidate=="Against all"
drop if Candidate=="N/A"
drop if Candidate=="N/A"
// Indirect election where one candidate was represented by several parties -- we keep only the highest ranked "version" of the candidate (n°2). The candidate indeed arrived second at the indirect election.
drop if Country=="Argentina" & Year==1989 & Candidate=="Eduardo Angeloz" & rank>2
tempfile election_results
save `election_results'

// Importing representatives of the incumbency
use "$project_path/data/3_cleaned/turnovers_presidential", clear
keep Country Year Month candidate_incumbency
ren candidate_incumbency Candidate
gen candidate_incumbency = 1
drop if Candidate==""
merge 1:1 Country Year Month Candidate using `election_results', assert(using match) nogen

// Keeping candidates ranked first or second, and candidates of the incumbency
keep if candidate_incumbency==1 | rank==1 | rank==2
gen election_winner = (rank==1)
gen election_runnerup = (rank==2)

// We remove elections which were inconsequential or not the last in their year
drop if flag_not_last==1
drop if flag_inconsequential==1
drop flag_not_last flag_inconsequential

// Merging with Wikidata IDs
merge 1:m Country Year Month Candidate using "$project_path/data/1_input/elections/Wikipedia_links/links_pres", keepusing(candidate_wikidata_id) assert(using match) keep(match) nogen
format Candidate %20s

// We find the number of terms already executed, as well as the number of immediate preceding terms, by looping over observations.
gen overall_terms = 0
gen immed_prev_terms = 0
local N = _N
gsort Country Year Month -election_winner Candidate
forvalues i = 1/`N' {
	local candidate_id = candidate_wikidata_id[`i']
	local term_break = 0
	if "`candidate_id'"!="" {
		forvalues j = 1/50 {
			qui replace overall_terms = overall_terms + 1 in `i' if candidate_wikidata_id[`i'-`j']=="`candidate_id'" & election_winner[`i'-`j']==1
			qui replace immed_prev_terms = immed_prev_terms + 1  in `i' if candidate_wikidata_id[`i'-`j']=="`candidate_id'" & election_winner[`i'-`j']==1 & `term_break'==0
			if candidate_wikidata_id[`i'-`j']!="`candidate_id'" & election_winner[`i'-`j']==1 & Year[`i']!=Year[`i'-`j'] {
				local term_break = 1
			}
		}
	}
}

// Changes to account for elections that took place before 1946
replace overall_terms = 1	if Country=="Brazil"	& Year==1950	& Candidate=="Getúlio D. Vargas"
replace immed_prev_terms = 0	if Country=="Brazil"	& Year==1950	& Candidate=="Getúlio D. Vargas"
replace overall_terms = 1	if Country=="Chile"	& Year==1952	& Candidate=="Carlos Ibáñez del Campo"
replace immed_prev_terms = 0	if Country=="Chile"	& Year==1952	& Candidate=="Carlos Ibáñez del Campo"
replace overall_terms = 1	if Country=="Cuba"	& Year==1954	& Candidate=="Fulgencio Batista"
replace immed_prev_terms = 0	if Country=="Cuba"	& Year==1954	& Candidate=="Fulgencio Batista"
replace overall_terms = 1	if Country=="Cuba"	& Year==1954	& Candidate=="Ramón Grau San Martín"
replace immed_prev_terms = 0	if Country=="Cuba"	& Year==1954	& Candidate=="Ramón Grau San Martín"
replace overall_terms = 3	if Country=="Dom. Rep."	& Year==1947	& Candidate=="Rafael L. Trujillo Molina"
replace immed_prev_terms = 1	if Country=="Dom. Rep."	& Year==1947	& Candidate=="Rafael L. Trujillo Molina"
replace overall_terms = 1	if Country=="Ecuador"	& Year==1952	& Candidate=="José María Velasco Ibarra"
replace immed_prev_terms = 0	if Country=="Ecuador"	& Year==1952	& Candidate=="José María Velasco Ibarra"
replace overall_terms = 1	if Country=="Finland"	& Year==1946	& Candidate=="Kaarlo Juho Ståhlberg"
replace immed_prev_terms = 0	if Country=="Finland"	& Year==1946	& Candidate=="Kaarlo Juho Ståhlberg"
replace overall_terms = 1	if Country=="Korea, Republic of"	& Year==1952	& Candidate=="Syngman Rhee"
replace immed_prev_terms = 1	if Country=="Korea, Republic of"	& Year==1952	& Candidate=="Syngman Rhee"
replace overall_terms = 1	if Country=="Nicaragua"	& Year==1950	& Candidate=="Anastasio Somoza García"
replace immed_prev_terms = 1	if Country=="Nicaragua"	& Year==1950	& Candidate=="Anastasio Somoza García"
replace overall_terms = 1	if Country=="Panama"	& Year==1948	& Candidate=="Arnulfo Arias Madrid"
replace immed_prev_terms = 0	if Country=="Panama"	& Year==1948	& Candidate=="Arnulfo Arias Madrid"
replace overall_terms = 2	if Country=="Paraguay"	& Year==1953	& Candidate=="Federico Chávez"
replace immed_prev_terms = 2	if Country=="Paraguay"	& Year==1953	& Candidate=="Federico Chávez"
replace overall_terms = 0	if Country=="Puerto Rico"	& Year==1952	& Candidate=="Luis Muñoz Marín"
replace immed_prev_terms = 0	if Country=="Puerto Rico"	& Year==1952	& Candidate=="Luis Muñoz Marín"

tempfile candidate_terms
save `candidate_terms'

////////////////////////////////////////////////////////////////////////////////
//////// b. Determining if a candidate would be facing a term limit if elected
////////////////////////////////////////////////////////////////////////////////

// We extract data on term limits from the CCP
use "$project_path/data/1_input/other/ccp/ccp_extract.dta", clear
ren country Country
ren year Year

// Harmonizing country names
replace Country="Antigua and Barbuda"  if Country=="Antigua And Barbuda"
replace Country="Austria"  if Country=="Austria-Hungary"
replace Country="Belarus"  if Country=="Belarus (Byelorussia)"
replace Country="Bosnia-Herz"  if Country=="Bosnia-Herzegovina"
replace Country="Burkina Faso"  if Country=="Burkina Faso (Upper Volta)"
replace Country="Cambodia"  if Country=="Cambodia (Kampuchea)"
replace Country="C. Verde Is."  if Country=="Cape Verde"
replace Country="Cent. Af. Rep."  if Country=="Central African Republic"
replace Country="PRC"  if Country=="China"
replace Country="Comoro Is."  if Country=="Comoros"
replace Country="Congo (DRC)"  if Country=="Congo, Democratic Republic Of (Zaire)"
replace Country="Ivory Coast"  if Country=="Cote D'Ivoire"
replace Country="Czech Rep."  if Country=="Czech Republic"
replace Country="Dom. Rep."  if Country=="Dominican Republic"
replace Country="Timor-Leste"  if Country=="East Timor"
replace Country="Eq. Guinea"  if Country=="Equatorial Guinea"
replace Country="GDR"  if Country=="German Democratic Republic"
replace Country="FRG/Germany"  if Country=="German Federal Republic"
replace Country="FRG/Germany"  if Country=="Germany (Prussia)"
replace Country="Iran"  if Country=="Iran (Persia)"
replace Country="Italy"  if Country=="Italy/Sardinia"
replace Country="PRK"  if Country=="Korea, People'S Republic Of"
replace Country="Korea, Republic of"  if Country=="Korea, Republic Of"
replace Country="Kyrgyzstan"  if Country=="Kyrgyz Republic"
replace Country="Macedonia"  if Country=="Macedonia (Former Yugoslav Republic Of)"
replace Country="Madagascar"  if Country=="Madagascar (Malagasy)"
replace Country="Micronesia, Federated States of"  if Country=="Micronesia, Fed. Sts."
replace Country="Myanmar"  if Country=="Myanmar (Burma)"
replace Country="P. N. Guinea"  if Country=="Papua New Guinea"
replace Country="Russia"  if Country=="Russia (Soviet Union)"
replace Country="Sao Tome and Principe"  if Country=="Sao Tome And Principe"
replace Country="Solomon Is."  if Country=="Solomon Islands"
replace Country="S. Africa"  if Country=="South Africa"
replace Country="Sri Lanka"  if Country=="Sri Lanka (Ceylon)"
replace Country="Saint Kitts and Nevis"  if Country=="St. Kitts And Nevis"
replace Country="Saint Vincent and The Grenadines"  if Country=="St. Vincent And The Grenadines"
replace Country="Suriname"  if Country=="Surinam"
replace Country="Tanzania"  if Country=="Tanzania/Tanganyika"
replace Country="Trinidad-Tobago"  if Country=="Trinidad And Tobago"
replace Country="Turkey"  if Country=="Turkey/Ottoman Empire"
replace Country="UAE"  if Country=="United Arab Emirates"
replace Country="UK"  if Country=="United Kingdom"
replace Country="USA"  if Country=="United States Of America"
replace Country="Vietnam"  if Country=="Vietnam, Democratic Republic Of"
replace Country="Yemen"  if Country=="Yemen (Arab Republic Of Yemen)"
replace Country="Serbia"  if Country=="Yugoslavia (Serbia)"
replace Country="Zimbabwe"  if Country=="Zimbabwe (Rhodesia)"
drop if Country=="Abkhazia"
drop if Country=="Baden"
drop if Country=="Bavaria"
drop if Country=="Great Colombia"
drop if Country=="Hanover"
drop if Country=="Hesse-Darmstadt (Ducal)"
drop if Country=="Hesse-Kassel (Electoral)"
drop if Country=="Mecklenburg-Schwerin"
drop if Country=="Orange Free State"
drop if Country=="Saxony"
drop if Country=="South Ossetia"
drop if Country=="Transvaal"
drop if Country=="United Provinces Of Central America"
drop if Country=="Vietnam, Republic Of"
drop if Country=="Wurttemberg"
drop if Country=="Yemen, People'S Republic Of"

// Merging with elections data
merge 1:m Country Year using `candidate_terms', keep(using match) nogen

// Merging with elected leader
merge m:1 Country Year Month Type_Election using "$project_path/data/2_intermediary/leaders/elected_leaders.dta", assert(using match) keep(match) nogen

// Finding CCP term limit code
gen terml = .
gen terml_comment = ""
replace terml = hogterml if elected_leader=="HOG"
replace terml = hosterml if elected_leader!="HOG"
replace terml_comment = hogterml_comments if elected_leader=="HOG"
replace terml_comment = hosterml_comments if elected_leader!="HOG"
drop evnttype hosterml hosterml_comments hogterml hogterml_comments elected_leader

*** Finding if a candidate would be facing a term limit 

gen face_term_limit = .

assert overall_terms!=.
assert immed_prev_terms!=.

// 1: Only one term permitted, total
replace face_term_limit=1 if terml==1
// 2: Only two terms permitted, total
replace face_term_limit=1 if terml==2 & overall_terms>=1 
replace face_term_limit=0 if terml==2 & overall_terms==0
// 3: No successive terms permitted, but multiple non-successive terms permitted
replace face_term_limit=1 if terml==3
// 4: Only two successive terms permitted, but multiple non-successive terms permitted
replace face_term_limit=1 if terml==4 & immed_prev_terms>=1
replace face_term_limit=0 if terml==4 & immed_prev_terms==0
// 5: No term limits
replace face_term_limit=0 if terml==5

// 90 and 96: look at comments
tab terml_comment if terml==90 |terml==96, m
replace face_term_limit = 1 if terml_comment=="three terms allowed" & overall_terms>=2
replace face_term_limit = 0 if terml_comment=="three terms allowed" & overall_terms<2
replace face_term_limit = 1 if terml_comment=="not more than 4 successive terms" & immed_prev_terms>=3
replace face_term_limit = 0 if terml_comment=="not more than 4 successive terms" & immed_prev_terms<3
replace face_term_limit = 1 if terml_comment=="no more than 3 successive reelections" & immed_prev_terms>=2
replace face_term_limit = 0 if terml_comment=="no more than 3 successive reelections" & immed_prev_terms<2
replace face_term_limit = 1 if terml_comment=="no more than 3 terms" & overall_terms>=2
replace face_term_limit = 0 if terml_comment=="no more than 3 terms" & overall_terms<2

// When no term limit is specified, we don't make an inference
replace face_term_limit = . if terml==98

// 99: not applicable
replace face_term_limit = . if terml==99

drop candidate_incumbency rank

tempfile term_limits
save `term_limits'

////////////////////////////////////////////////////////////////////////////////
//////// c. Assigning term limit to representative of the incumbency/opposition
////////////////////////////////////////////////////////////////////////////////

use "$project_path/data/3_cleaned/turnovers_presidential", clear
drop if flag_not_last==1
drop if flag_inconsequential==1
drop if candidate_incumbency=="" | candidate_opposition==""
keep Country Year Month Type_Election candidate_incumbency candidate_opposition
ren candidate_incumbency Candidate
merge 1:1 Country Year Month Type_Election Candidate using `term_limits', assert(using match) keep(match) keepusing(face_term_limit) nogen
ren face_term_limit face_term_limit_incumbency
ren Candidate candidate_incumbency
ren candidate_opposition Candidate
merge 1:1 Country Year Month Type_Election Candidate using `term_limits', assert(using match) keep(match) keepusing(face_term_limit) nogen
ren face_term_limit face_term_limit_opposition
ren Candidate candidate_opposition

lab var face_term_limit_incumbency "Representative of the incumbency will face term limit if elected"
lab var face_term_limit_opposition "Representative of the opposition will face term limit if elected"

*** Saving the output
keep Country Year Month Type_Election face_term_limit_incumbency face_term_limit_opposition
order Country Year Month Type_Election face_term_limit_incumbency face_term_limit_opposition
isid Country Year Month Type_Election, m
compress
label data ""
notes drop _dta

save "$project_path/data/2_intermediary/elections/Presidential elections/term_limits", replace

